package com.li.shanlin.controller;

import java.io.File;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.li.shanlin.baseUtils.FacePlusUtils;
import com.li.shanlin.utils.BaseFileUtil;

/**
 * @author liyinlong
 *
 *         2017年3月9日下午12:59:25
 */
@RestController
@RequestMapping("/face")
public class DetectController {
	@RequestMapping("/card")
	public String getDetectPic(@RequestParam("image") MultipartFile image,@RequestParam("shenfen") MultipartFile shenfen, HttpServletRequest request) {
		String filePath1 = null;
		String filePath2 = null;
		if (!image.isEmpty()) {  
            try {  
                // 文件保存路径  
                filePath1 = request.getSession().getServletContext().getRealPath("/")  + "static/upload/"
                        + image.getOriginalFilename(); 
                System.out.println("filePath1:"+filePath1);
                // 转存文件  
                image.transferTo(new File(filePath1));  
            } catch (Exception e) {  
                e.printStackTrace();  
            }  
        }
		if (!shenfen.isEmpty()) {  
            try {  
                // 文件保存路径  
                filePath2 = request.getSession().getServletContext().getRealPath("/") + "static/upload/"
                        + shenfen.getOriginalFilename();
                System.out.println("filePath2:"+filePath2);
                // 转存文件  
                shenfen.transferTo(new File(filePath2));  
            } catch (Exception e) {  
                e.printStackTrace();  
            }  
        }
		File image1 = new File(filePath1);
		File shenfen1 = new File(filePath2);
		String result2 = FacePlusUtils.faceOcridcard(shenfen1);
		//打印face_token
		//打印身份信息
		Map<String,Object> json3 = JSON.parseObject(result2);
		String idcard_name = (String) json3.get("name");
		String idcard_number = (String) json3.get("id_card_number");
		String activeAndRawimage = FacePlusUtils.faceVerifyActiveAndRawimage(idcard_name, idcard_number,image1);
		return activeAndRawimage;
	}
	@RequestMapping("/card1")
	public String getDetectPic1(@RequestParam("image") MultipartFile image,@RequestParam("shenfen") MultipartFile shenfen, HttpServletRequest request) {
		String filePath1 = null;
		String filePath2 = null;
		if (!image.isEmpty()) {  
            try {  
                // 文件保存路径  
                filePath1 = request.getSession().getServletContext().getRealPath("/") + "static/upload/"
                        + image.getOriginalFilename();  
                // 转存文件  
                image.transferTo(new File(filePath1));  
            } catch (Exception e) {  
                e.printStackTrace();  
            }  
        }
		if (!shenfen.isEmpty()) {  
            try {  
                // 文件保存路径  
                filePath2 = request.getSession().getServletContext().getRealPath("/") + "static/upload/"  
                        + shenfen.getOriginalFilename();  
                // 转存文件  
                shenfen.transferTo(new File(filePath2));  
            } catch (Exception e) {  
                e.printStackTrace();  
            }  
        }
		File image1 = new File(filePath1);
		File shenfen1 = new File(filePath2);
		String result1 = FacePlusUtils.faceDetectMess(image1);
		String result2 = FacePlusUtils.faceOcridcard(shenfen1);
		//打印face_token
		JSONObject json1 = JSON.parseObject(result1);
		List<Map<String,Object>> json2 = (List<Map<String, Object>>) json1.get("faces");
		Map<String,Object> faces = json2.get(0);
		String face_token = (String) faces.get("token");		
		//打印身份信息
		Map<String,Object> json3 = JSON.parseObject(result2);
		String idcard_name = (String) json3.get("name");
		String idcard_number = (String) json3.get("id_card_number");
		String activeAndFacetoken = FacePlusUtils.faceVerifyActiveAndFacetoken(idcard_name, idcard_number, face_token);
		System.out.println("activeAndFacetoken:"+activeAndFacetoken);
		return activeAndFacetoken;
	}
	/**
	 * 这样转base64
	 * @param shenfen
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/card2")
	public String getDetectPic2(@RequestParam("shenfen") MultipartFile shenfen, HttpServletRequest request) throws Exception {
		String filePath2 = null;
		if (!shenfen.isEmpty()) {  
            try {  
                // 文件保存路径  
                filePath2 = request.getSession().getServletContext().getRealPath("/") + "static/upload/"  
                        + shenfen.getOriginalFilename();  
                // 转存文件  
                shenfen.transferTo(new File(filePath2));  
            } catch (Exception e) {  
                e.printStackTrace();
            }  
        }
		File shenfen1 = new File(filePath2);
		String s = null;
		try {
			s = BaseFileUtil.encodeBase64File(filePath2);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		String filePath = request.getSession().getServletContext().getRealPath("/") + "static/upload2/liyinlong.jpg";  
		BaseFileUtil.decoderBase64File(s, filePath);
		File newFile = new File(filePath);
		String result2 = FacePlusUtils.faceOcridcard(newFile);
		/*//打印face_token
		JSONObject json1 = JSON.parseObject(result1);
		List<Map<String,Object>> json2 = (List<Map<String, Object>>) json1.get("faces");
		Map<String,Object> faces = json2.get(0);
		String face_token = (String) faces.get("token");		
		//打印身份信息
		Map<String,Object> json3 = JSON.parseObject(result2);
		String idcard_name = (String) json3.get("name");
		String idcard_number = (String) json3.get("id_card_number");
		String activeAndFacetoken = FacePlusUtils.faceVerifyActiveAndFacetoken(idcard_name, idcard_number, face_token);
		System.out.println("activeAndFacetoken:"+activeAndFacetoken);*/
		return result2;
	}
	@RequestMapping("/card3")
	public String getDetectPic3(@RequestParam("shenfen") MultipartFile shenfen, HttpServletRequest request) throws Exception {
		String filePath2 = null;
		if (!shenfen.isEmpty()) {  
            try {  
                // 文件保存路径  
                filePath2 = request.getSession().getServletContext().getRealPath("/") + "static/upload/"  
                        + shenfen.getOriginalFilename();  
                // 转存文件  
                shenfen.transferTo(new File(filePath2));  
            } catch (Exception e) {  
                e.printStackTrace();
            }  
        }
		String s = null;
		try {
			s = BaseFileUtil.encodeBase64File(filePath2);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		/*String filePath = request.getSession().getServletContext().getRealPath("/") + "static/upload2/liyinlong.jpg";  
		BaseFileUtil.decoderBase64File(s, filePath);
		File newFile = new File(filePath);
		String result2 = FacePlusUtils.faceOcridcard(newFile);*/
		return s;
	}
	@RequestMapping("/card4")
	public String getDetectPic4(@RequestParam("shenfen") String shenfen, HttpServletRequest request) throws Exception {
		String filePath2 = request.getSession().getServletContext().getRealPath("/") + "static/upload/liyinlong1.jpg";
             
		BaseFileUtil.decoderBase64File(shenfen, filePath2);
		File newFile = new File(filePath2);
		String result2 = FacePlusUtils.faceOcridcard(newFile);
		return result2;
	}
}
